package com.qms.repast.aspectj.login;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.qms.repast.ui.activity.LoginActivity;
import com.qms.repast.utils.ToastUtils;
import com.qms.repast.utils.UserSpUtils;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

/**
 * @author 泅渡者
 * @date 2018/8/29 0029
 * @describe:
 */
@Aspect
public class CheckLoginAspectJ {
	private static final String TAG = "CheckLogin";

	/**
	 * 找到处理的切点 * *(..)  可以处理CheckLogin这个类所有的方法
	 */
	@Pointcut("execution(@com.qms.repast.aspectj.login.CheckLogin  * *(..))")
	public void executionCheckLogin () {
	}

	/**
	 * 处理切面
	 *
	 * @param joinPoint
	 *
	 * @return
	 */
	@Around("executionCheckLogin()")
	public Object checkLogin (ProceedingJoinPoint joinPoint) throws Throwable {
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		CheckLogin checkLogin = signature.getMethod().getAnnotation(CheckLogin.class);
		if (checkLogin != null) {
			Context context = (Context) joinPoint.getThis();
			if (UserSpUtils.isLogin(context) == true) {
				Log.i(TAG, "checkLogin: 已登录 ");
				return joinPoint.proceed();
			} else {
				Log.i(TAG, "checkLogin: 请登录");
				ToastUtils.showToast("请登录");
				Intent intent = new Intent(context, LoginActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent);
				return null;
			}
		}
		return joinPoint.proceed();
	}
}
